package cn.est.service.impl;
import cn.est.dto.BrandDto;
import cn.est.dto.ClassfyDto;
import cn.est.mapper.ClassifyMapper;
import cn.est.pojo.Brand;
import cn.est.pojo.Classify;
import cn.est.pojo.Model;
import cn.est.service.BrandService;
import cn.est.service.ClassifyService;
import cn.est.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import cn.est.dto.Page;
import cn.est.constants.Constants;
@Service
public class ClassifyServiceImpl implements ClassifyService {

    @Autowired
    private ClassifyMapper classifyMapper;

    @Autowired
    private BrandService brandService;

    @Autowired
    private  ModelService modelService;


    public Classify getClassifyById(Long id){
        return classifyMapper.getClassifyById(id);
    }

    public List<Classify>	getClassifyListByMap(Map<String,Object> param){
        return classifyMapper.getClassifyListByMap(param);
    }

    public Integer getClassifyCountByMap(Map<String,Object> param){
        return classifyMapper.getClassifyCountByMap(param);
    }

    public Integer qdtxAddClassify(Classify classify){
        classify.setCreatdTime(new Date());
        return classifyMapper.insertClassify(classify);
    }

    public Integer qdtxModifyClassify(Classify classify){
        classify.setUpdatedTime(new Date());
        return classifyMapper.updateClassify(classify);
    }

    public Integer qdtxDeleteClassifyById(Long id){
        return classifyMapper.deleteClassifyById(id);
    }

    public Integer qdtxBatchDeleteClassify(String ids){
        Map<String,List<String>> param=new HashMap<String,List<String>>();
        String[] paramArrays=ids.split(",");
        List<String> idList=new ArrayList<String>();
        for (String temp:paramArrays){
            idList.add(temp);
        }
        param.put("ids",idList);
        return classifyMapper.batchDeleteClassify(param);
    }

    public Page<Classify> queryClassifyPageByMap(Map<String,Object> param,Integer pageNo,Integer pageSize){
        Integer total = classifyMapper.getClassifyCountByMap(param);
        pageNo = pageNo==null ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = pageSize==null ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Classify> classifyList = classifyMapper.getClassifyListByMap(param);
        page.setList(classifyList);
        return page;
    }

    @Override
    public List<ClassfyDto> queryClassfytree() {
        List<ClassfyDto> classfyDtosList=null;


        //1.获取所有的分类信息
        classfyDtosList =classifyMapper.selectlist();


        //2.遍历所有分类的类信息，获取类所对应的品牌
        for (ClassfyDto classfyDto:
                classfyDtosList) {
            if(classfyDto!=null&&classfyDto.getId()!=null){
                Integer cid=classfyDto.getId();
                List<BrandDto> brandDtoList=brandService.getListByCid(cid);
                for(BrandDto brandDto:brandDtoList){
                    //3.遍历品牌信息,获取商品信息
                    Integer bid=brandDto.getId();
                    Map map =new HashMap();
                    map.put("classifyId",cid);
                    map.put("brandId",bid);
                    List<Model> modelList= modelService.getModelListByMap(map);
                    brandDto.setModelList(modelList);
                }
                classfyDto.setBrandDtos(brandDtoList);
            }
        }
        //4.封装好数据就返回
        return classfyDtosList;
    }


}
